package com.ruix.app.ui.stratum.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.ruix.app.R;
import com.ruix.app.mapper.FeedbackListMapper;
import com.ruix.app.model.Classes;
import com.ruix.app.model.FeedbackAdvice;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.FeedbackListAdapter;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.widget.SwipeListView;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/25.
 */

public class FeadbackAdviceActivity extends BaseActivity{

    public static Intent newIntent(Context context, Classes classes) {
        Intent intent = new Intent(context, FeadbackAdviceActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("classes", classes);
        intent.putExtras(bundle);
        return intent;
    }


    private View noMessageTipView;
    private ListView advicelist;
    private Classes currentClasses;
    private SpringView springView;
    private FeedbackListAdapter adapter;
    private static final int LOAD_COUNT = 20;
    private SwipeRefreshLayout mRefreshLayout;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_class_feadback_info;
    }

    @Override
    protected void initView() {
        setTitle("反馈意见");

        advicelist = (ListView) findViewById(R.id.lv_advice_list);
        noMessageTipView = findViewById(R.id.tv_no_data_tip);

        adapter = new FeedbackListAdapter(this);
        advicelist.setAdapter(adapter);

        springView = (SpringView) findViewById(R.id.spring_view);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setGive(SpringView.Give.BOTTOM);
//        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        mRefreshLayout.setColorSchemeResources(R.color.bg_nav_default
                , R.color.bg_nav_yellow
                , R.color.bg_default
                , R.color.text_color_pink);
    }

    @Override
    protected void initListener() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadFeadbackList(LOAD_COUNT,0,false);
            }
        });
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {

            }
            @Override
            public void onLoadmore() {
                loadFeadbackList(LOAD_COUNT,adapter.getCount(),false);
            }
        });
        advicelist.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getFirstVisiblePosition() == 0) {
                        mRefreshLayout.setEnabled(true);
                    } else {
                        mRefreshLayout.setEnabled(false);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }


    @Override
    protected void initLocalData() {
        Intent intent = getIntent();
        if (intent != null){
            currentClasses =  (Classes)intent.getSerializableExtra("classes");
        }
        loadFeadbackList(LOAD_COUNT,0,true);
    }


    private void loadFeadbackList(int limit, final int offset, final boolean isFirst){
        if (isFirst) {
            showLoading();
        }
        RequestFactory.feedbackList(currentClasses.getID(), limit, offset, new FeedbackListMapper(), new RXObserver<ArrayList<FeedbackAdvice>>() {

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<FeedbackAdvice> value) {
                if (value.size() >0){
                    hideTipView();
                    if (offset > 0) {
                        adapter.addAll(value);
                    } else {
                        adapter.replaceAll(value);
                    }
                }
                if (isFirst) {
                    if (value.size() == 0){
                        showNoMessageTipView();
                    }
                    dismissLoading();
                } else {
                    if (value.size() ==0){
                       showErrorTip("没有更多消息");
                    }
                    mRefreshLayout.setRefreshing(false);
                    springView.onFinishFreshAndLoad();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (isFirst) {
                    dismissLoading();
                } else {
                    mRefreshLayout.setRefreshing(false);
                    springView.onFinishFreshAndLoad();
                }
                resolveException(e);
            }

            @Override

            public void onComplete() {


            }
        });
    }

    private void showNoMessageTipView() {
        advicelist.setVisibility(View.GONE);
        noMessageTipView.setVisibility(View.VISIBLE);
    }

    private void hideTipView() {
        noMessageTipView.setVisibility(View.GONE);
        advicelist.setVisibility(View.VISIBLE);
    }

}
